package cn.net.isir.utils;

import org.apache.logging.log4j.util.PropertiesUtil;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;

/**
 * @author liuxubo
 * Email: lxb@isir.net.cn
 * Package: cn.net.isir.common
 * Filename: PropertiesUtils.java
 * Comment: load the properties config file
 * @since 1.8
 * Date: 2018/11/14
 */
public class PropertiesUtils {

    /**
     * 加载文件中的所有key
     *
     * @param fileName 文件名
     * @return 所有key-value的map
     */
    public static Map<String, String> loadAllProperties(String fileName) {
        InputStream is = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName);
        if (is == null) {
            return null;
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        Properties pr = new Properties();
        try {
            pr.load(br);
            Map<String, String> map = new HashMap<>();
            for (Object key : pr.keySet()) {
                map.put(key + "", pr.getProperty(key + ""));
            }
            return map;
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 加载文件中的指定key
     *
     * @param fileName 文件名
     * @param key      key
     * @return 结果
     */
    public static String loadProperties(String fileName, String key) {
        InputStream is = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName);
//        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        if (is == null) {
            return null;
        }
        InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
        Properties pr = new Properties();
        try {
            pr.load(isr);
            if (pr.containsKey(key)) {
                return pr.get(key) + "";
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                isr.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 设置文件
     *
     * @param fileName 文件名
     * @param map      key-value
     */
    private void set(String fileName, HashMap<String, String> map) {
        InputStream is = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName);
        if (is == null) {
            return;
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        Properties pr = new Properties();
        String filePath = Objects.requireNonNull(PropertiesUtil.class.getClassLoader().getResource(fileName)).getFile();
        try {
            pr.load(br);
            pr.putAll(map);
            OutputStream out = new FileOutputStream(filePath);
            pr.save(out, "");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
//        logger.info(loadProperties("wx-keys.properties", "appid"));
    }

}
